/**
 * Copyright (c) 2010 Jesco Freund.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of Backtory nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *  $Id$
 */


#ifndef __LIBYAR_FHEADER_H_
#define __LIBYAR_FHEADER_H_

#include <stddef.h>
#include <stdint.h>
#include <libyar/types.h>
#include <libyar/version.h>

/* size of a yar archive file header, indicated in bytes */
#define LIBYAR_FH_BYTES 32U

/* sequence building the first three bytes of a yar archive file */
#define LIBYAR_FH_IDENTIFIER 0x594152U

/* processing flag components */
#define LIBYAR_FH_MDT_COMPRESS	0x1000
#define LIBYAR_FH_MDT_ENCRYPT 	0x2000
#define LIBYAR_FH_FMD_COMPRESS	0x0100
#define LIBYAR_FH_FMD_ENCRYPT	0x0200
#define LIBYAR_FH_FMD_DIGEST	0x0400
#define LIBYAR_FH_FCD_COMPRESS	0x0010
#define LIBYAR_FH_FCD_ENCRYPT	0x0020
#define LIBYAR_FH_FCD_DIGEST	0x0040
#define LIBYAR_FH_FI_COMPRESS	0x0001
#define LIBYAR_FH_FI_ENCRYPT	0x0002

typedef struct
{
	uint32_t identifier;
	uint64_t version;
	uint8_t valid;
} yar_header_t;


/**
 * Macro to initialize header data structure
 */
#define yar_header_init(header) { \
	(header)->identifier = LIBYAR_FH_IDENTIFIER; \
	(header)->version = LIBYAR_VERSION_NUM; \
	(header)->valid = 1; \
}

/**
 * Read header from file named filename and store it in the
 * header data structure. Mark whether this is a valid header
 * or not.
 */
extern int yar_fh_read_fn(const char *filename, yar_header_t *header);

/**
 * Read header from file designated by the fd file descriptor
 * and store it in the header data structure. 
 * Mark whether this is a valid header or not.
 */
extern int yar_fh_read_fd(int fd, yar_header_t *header);

/**
 * Read header from buffer buf with a size of len bytes
 * and store it in the header data structure. 
 * Mark whether this is a valid header or not.
 */
extern int yar_fh_read_buf(const unsigned char *buf, size_t len, yar_header_t *header);

/**
 * Create header in buf, based on the information provided
 * by the header data structure. buf must be at least 8 bytes long.
 */
extern int yar_fh_write_buf(void *buf, size_t len, yar_header_t *header);

#endif /* __LIBYAR_FHEADER_H_ */
